package com.vr.xuecheng.content.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vr.xuecheng.base.exception.XueChengPlusException;
import com.vr.xuecheng.content.mapper.CourseAuditMapper;
import com.vr.xuecheng.content.model.dto.CourseAuditDTO;
import com.vr.xuecheng.content.model.entity.CourseAudit;
import com.vr.xuecheng.content.model.entity.CourseBase;
import com.vr.xuecheng.content.model.entity.CoursePublishPre;
import com.vr.xuecheng.content.model.entity.CourseTeacher;
import com.vr.xuecheng.content.model.vo.CourseBaseOfPreviewVO;
import com.vr.xuecheng.content.service.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hsh
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CourseAuditServiceImpl extends ServiceImpl<CourseAuditMapper, CourseAudit> implements ICourseAuditService {
//    private final CourseAuditMapper courseAuditMapper;
    private final ICourseBaseService courseBaseService;
    private final ICoursePublishPreService coursePublishPreService;
    private final ICourseMarketService marketService;
    private final ITeachPlanService teachPlanService;
    private final ICourseTeacherService courseTeacherService;
    private final CourseAuditMapper courseAuditMapper;

    @Transactional
    @Override
    public boolean commitAudit(Long courseId) {
        // 获取待提交的课程信息
        CourseBaseOfPreviewVO waitCourseBase = courseBaseService.getCourseBaseInfoById(courseId);
        if (waitCourseBase == null) {
            XueChengPlusException.cast("提交失败，课程不存在");
        }
        // 判断当前待提交的课程的审核状态是否是已提交
        if ("202003".equals(waitCourseBase.getAuditStatus())) {
            // 表示有审核人员在审核或者等待审核
            XueChengPlusException.cast("当前课程正在审核中或等待审核中，请不要重复提交");
        }
        // 校验课程是否包含图片
        if (StrUtil.isBlank(waitCourseBase.getPic())) {
            XueChengPlusException.cast("提交失败，请上传课程图片");
        }
        // 当前课程只能提交自己的课程
        // todo 待登录后获取
        Long companyId = 1232141425L;
        if (!companyId.equals(waitCourseBase.getCompanyId())) {
            XueChengPlusException.cast("不允许提交其他机构的课程");
        }
        List<Tree<Long>> teachPlanTree = teachPlanService.getTreeNodesById(courseId);
        if (teachPlanTree == null || teachPlanTree.isEmpty()) {
            XueChengPlusException.cast("提交失败，还没添加课程计划");
        }
        // 校验完成，将待提交的课程信息保存到预发布表里
        // 组装数据
        List<CourseTeacher> courseTeachers = courseTeacherService.lambdaQuery().eq(CourseTeacher::getCourseId, courseId).list();
        if (courseTeachers == null || courseTeachers.isEmpty()) {
            XueChengPlusException.cast("提交失败，当前课程还没有授课老师");
        }
        String marketJson = JSON.toJSONString(marketService.getById(courseId));
        String teachPlanJson = JSON.toJSONString(teachPlanTree);
        String courseTeacherJson = JSON.toJSONString(courseTeachers);
        CoursePublishPre coursePublishPre = BeanUtil.toBean(waitCourseBase, CoursePublishPre.class)
                .setMarket(marketJson)
                .setTeachplan(teachPlanJson)
                .setTeachers(courseTeacherJson)
                .setCreateDate(LocalDateTime.now())
                // 更新状态为已提交
                .setStatus("202003");
        // 根据课程id查询预发布表，如果存在则更新，不存在则新增
        boolean isPublish = coursePublishPreService.saveOrUpdate(coursePublishPre);
        if (isPublish) {
            // 更新课程基本信息表
            courseBaseService.lambdaUpdate()
                    .eq(CourseBase::getId, courseId)
                    .set(CourseBase::getAuditStatus, "202003")
                    .update();
        }
        return true;
    }

    @Override
    @Transactional
    public boolean completeAudit(CourseAuditDTO req) {
        courseAuditMapper.insert(BeanUtil.toBean(req, CourseAudit.class)
                .setAuditDate(LocalDateTime.now())
                .setAuditPeople("test"));
        courseBaseService.lambdaUpdate()
                .eq(CourseBase::getId, req.getCourseId())
                .set(CourseBase::getAuditStatus, req.getAuditStatus())
                .update();
        return true;
    }
}
